import { getEntireRoomList } from "@/services";
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";

export const fetchRoomListAction = createAsyncThunk(
  "fetchroom",
  (payload, { dispatch }) => {
    dispatch(changeIsLoadingAction(true));
    getEntireRoomList().then((res) => {
      dispatch(changeRoomListAction(res));
      dispatch(changeIsLoadingAction(false));
    });
  }
);

const entireSlice = createSlice({
  name: "entire",
  initialState: {
    currentPage: 0, // 当前页码
    roomList: [], // 房间列表
    totalCount: 0, // 总数据个数
    isLoading: false,
  },
  reducers: {
    changeRoomListAction(state, { payload }) {
      state.roomList = payload.list;
      state.totalCount = payload.totalCount;
    },
    changeCurrentPageAction(state, { payload }) {
      state.currentPage = payload;
    },
    changeIsLoadingAction(state, { payload }) {
      state.isLoading = payload;
    },
  },
});

export const {
  changeCurrentPageAction,
  changeRoomListAction,
  changeIsLoadingAction,
} = entireSlice.actions;
export default entireSlice.reducer;
